import { Global, Module } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import * as mysql from 'mysql2/promise';
import { createConnectionPool } from './mysql.connection';

export const MYSQL_CONNECTION_POOL = 'MYSQL_CONNECTION_POOL';

@Global()   // 一次导入，处处可用
@Module({
  providers: [
    {
      provide: MYSQL_CONNECTION_POOL,
      useFactory: (config: ConfigService) => createConnectionPool(config),
      inject: [ConfigService],
    },
  ],
  exports: [MYSQL_CONNECTION_POOL], // 重点：导出令牌
})
export class DatabaseModule {}